package com.example.sportboot.controller;

import com.alibaba.fastjson.JSON;
import com.example.sportboot.bean.User;
import com.example.sportboot.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;



//返回字符
@RestController
public class LoginController {
//    注入实体类@Autowired自动注入
    @Autowired
    UserDao userDao;
    String flag="error";
//    使用RequestBody注解主要是为了可以找到user试题
//    使用@RequestMapping("/login")设置访问路径
    @RequestMapping("/login")
    public String Login(@RequestBody User user){
        System.out.printf("U111");
        System.out.println("User = " + user);
        User user1=userDao.getUserByMessage(user.getUsername(), user.getPassword());
        HashMap<String ,Object> res=new HashMap<String ,Object>();

        if(user1!=null){
//            将对象转换为json
            flag="ok";
        }else{
            flag="false";
        }
        res.put("flag",flag);
        res.put("user",user1);
        String res_json = JSON.toJSONString(res);
        return res_json;
    }
}
